﻿
Partial Public Class distributor
    Protected Class aggregate_internal
        Public Shared Sub assert_input(ByVal targetJobs() As indexedJob, ByVal inputJob As ijob)
            assert(Not targetJobs Is Nothing AndAlso targetJobs.Length() > 0)
            assert(Not inputJob Is Nothing)
        End Sub

        Public Shared Sub assert_input(ByVal targetJobs() As indexedJob,
                                       ByVal jobqueues() As jobqueue,
                                       ByVal queue_indexes() As UInt32,
                                       ByVal inputJob As ijob)
            assert_input(targetJobs, inputJob)
            assert(Not jobqueues Is Nothing)
            assert(Not queue_indexes Is Nothing)
            assert(jobqueues.Length() = targetJobs.Length())
            assert(jobqueues.Length() = targetJobs.Length())
        End Sub
    End Class

    Protected Shared Function unique_aggregate(ByVal targetJobs() As indexedJob,
                                               ByVal inputJob As ijob) As Boolean
        aggregate_internal.assert_input(targetJobs, inputJob)
        assert(targetJobs.Length() = 1)
        If targetJobs(0).finished() Then
            Return inputJob.copy(targetJobs(0))
        Else
            Return False
        End If
    End Function

    Protected Shared Function first_finish_aggregate(ByVal targetJobs() As indexedJob,
                                                     ByVal inputJob As ijob) As Boolean
        aggregate_internal.assert_input(targetJobs, inputJob)
        For i As Int64 = 0 To targetJobs.Length() - 1
            If targetJobs(i).finished() Then
                Return inputJob.copy(targetJobs(i).job())
            End If
        Next

        Return False
    End Function
End Class
